Completed
Push — master ( a5e25e...81f0bb )
by Emil
03:07
created

copier.js ➔ ... ➔ ???   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 43
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 1.048

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 1
eloc 38
nc 1
nop 0
dl 0
loc 43
ccs 14
cts 22
cp 0.6364
crap 1.048
rs 8.968
c 4
b 0
f 0
1
/* global it describe before */
2
3 1
process.env.NODE_ENV = 'test';
4
5
//Require the dev-dependencies
6 1
const chai = require('chai');
7 1
const chaiHttp = require('chai-http');
8 1
const HTMLParser = require('node-html-parser');
9
10 1
const server = require('../../app.js');
11
12 1
chai.should();
13
14 1
const db = require("../db/database.js");
15
16 1
const { exec } = require('child_process');
17
18 1
chai.use(chaiHttp);
19
20 1
let apiKey = "";
21
22 1
describe('copier', () => {
23 1
    before(() => {
24 1
        return new Promise((resolve) => {
25 1
            db.run("DELETE FROM apikeys", (err) => {
26 2
                if (err) {
27
                    console.error("Could not empty test DB table orders", err.message);
28
                }
29
30 1
                db.run("DELETE FROM products", (err) => {
31 2
                    if (err) {
32
                        console.error("Could not empty test DB table orders", err.message);
33
                    }
34
35 1
                    db.run("DELETE FROM orders", (err) => {
36 2
                        if (err) {
37
                            console.error("Could not empty test DB table orders", err.message);
38
                        }
39
40 1
                        db.run("DELETE FROM order_items", (err) => {
41 2
                            if (err) {
42
                                console.error("Could not empty test DB table orders", err.message);
43
                            }
44
45 1
                            exec(
46
                                'cat v2/db/seed.sql | sqlite3 v2/db/test.sqlite',
47
                                (error, stdout, stderr) => {
48 2
                                    if (error) {
49
                                        console.error(error.message);
50
                                        return;
51
                                    }
52
53 2
                                    if (stderr) {
54
                                        console.error(stderr);
55
                                        return;
56
                                    }
57
58 1
                                    resolve();
59
                                });
60
                        });
61
                    });
62
                });
63
            });
64
        });
65
    });
66
67 1
    describe('POST /copier/products', () => {
68 1
        it('should get 401 as we do not provide valid api_key', (done) => {
69 1
            chai.request(server)
70
                .post("/v2/copier/products")
71
                .end((err, res) => {
72 1
                    res.should.have.status(401);
73 1
                    res.body.should.be.an("object");
74 1
                    res.body.errors.status.should.be.equal(401);
75
76 1
                    done();
77
                });
78
        });
79
80 1
        it('should get 200 as we get apiKey', (done) => {
81 1
            let user = {
82
                email: "[email protected]",
83
                gdpr: "gdpr"
84
            };
85
86 1
            chai.request(server)
87
                .post("/v2/auth/api_key/confirmation")
88
                .send(user)
89
                .end((err, res) => {
90 1
                    res.should.have.status(200);
91
92 1
                    let HTMLResponse = HTMLParser.parse(res.text);
93 1
                    let apiKeyElement = HTMLResponse.querySelector('#apikey');
94
95 1
                    apiKey = apiKeyElement.childNodes[0].rawText;
96
97 1
                    done();
98
                });
99
        });
100
101 1
        it('should get 201 HAPPY PATH, 10 products should have been created', (done) => {
102 1
            chai.request(server)
103
                .post("/v2/copier/products")
104
                .send({ api_key: apiKey })
105
                .end((err, res) => {
106 1
                    res.should.have.status(201);
107 1
                    res.body.should.be.an("object");
108 1
                    res.body.data.should.be.an("array");
109 1
                    res.body.data.length.should.equal(10);
110
111 1
                    done();
112
                });
113
        });
114
115 1
        it('should get 201 HAPPY PATH, only 10 in response', (done) => {
116 1
            chai.request(server)
117
                .post("/v2/copier/products")
118
                .send({ api_key: apiKey })
119
                .end((err, res) => {
120 1
                    res.should.have.status(201);
121 1
                    res.body.should.be.an("object");
122 1
                    res.body.data.should.be.an("array");
123 1
                    res.body.data.length.should.equal(10);
124
125 1
                    done();
126
                });
127
        });
128
    });
129
130 1
    describe("POST /copy_all", () => {
131 1
        it('should get 401 as we do not provide valid api_key', (done) => {
132 1
            chai.request(server)
133
                .post("/v2/copier/all")
134
                .end((err, res) => {
135 1
                    res.should.have.status(401);
136 1
                    res.body.should.be.an("object");
137 1
                    res.body.errors.status.should.be.equal(401);
138
139 1
                    done();
140
                });
141
        });
142
143 1
        it('should get 200 as we get apiKey', (done) => {
144 1
            let user = {
145
                email: "[email protected]",
146
                gdpr: "gdpr"
147
            };
148
149 1
            chai.request(server)
150
                .post("/v2/auth/api_key/confirmation")
151
                .send(user)
152
                .end((err, res) => {
153 1
                    res.should.have.status(200);
154
155 1
                    let HTMLResponse = HTMLParser.parse(res.text);
156 1
                    let apiKeyElement = HTMLResponse.querySelector('#apikey');
157
158 1
                    apiKey = apiKeyElement.childNodes[0].rawText;
159
160 1
                    done();
161
                });
162
        });
163
164 1
        it('should get 201 HAPPY PATH', (done) => {
165 1
            chai.request(server)
166
                .post("/v2/copier/all")
167
                .send({ api_key: apiKey })
168
                .end((err, res) => {
169 1
                    res.should.have.status(201);
170 1
                    res.body.should.be.an("object");
171 1
                    res.body.data.should.have.property("products");
172 1
                    res.body.data.should.have.property("orders");
173
174 1
                    res.body.data.products.should.be.an("array");
175 1
                    res.body.data.products.length.should.equal(10);
176
177 1
                    res.body.data.orders.should.be.an("array");
178 1
                    res.body.data.orders.length.should.equal(4);
179
180 1
                    res.body.data.orders[0].order_items.should.be.an("array");
181 1
                    res.body.data.orders[0].order_items.length.should.equal(2);
182
183 1
                    done();
184
                });
185
        });
186
    });
187
});
188